package com.gdit.zax.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.PlanDao;
import com.gdit.zax.entitys.TaskAll;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.DateUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.VeriFicUtil;

public class getAllPlan extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String accesstoken = request.getParameter("accesstoken");
		int page = TextUtil.String2int(request.getParameter("page"), 1);
		int uid = VeriFicUtil.getVFID(accesstoken);
		
		//检测连接、用户
		if (uid==Integer.MIN_VALUE || uid == Integer.MAX_VALUE) {
			O.printNoLogin(response);
			return;
		}
		
		Connection conn = DBUtil.getConnection();
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}

		//开始
		DAOFactory dao = DAOFactory.init(conn, request, response);
		
		PlanDao pdao = dao.getPlanDao();
		ArrayList<TaskAll> list  = pdao.listTask(uid, page, 1000);
		
		ArrayList<Map<String, Object>> out = new ArrayList<Map<String,Object>>();
		for (TaskAll plan : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", plan.getT_course_id());
			map.put("planid", plan.getT_plan_id());
			map.put("theme", plan.getT_plan_content());
			map.put("plan", plan.getT_plan_name());
			map.put("remindType", plan.getT_plan_type());
			map.put("startTime", DateUtil.toDate(plan.getT_plan_addtime()));
			map.put("countdown", plan.get_countdown());
			map.put("overseer", plan.getT_plan_overseer());
			map.put("endTime", plan.getT_plan_plantime().getTime());
			out.add(map);
		}
		O.printSuccess(response, out);
		
		
		
	}

}
